Previous Book Contents Book Index Next

Inside Macintosh: QuickDraw GX Objects /
Chapter 4 - Colors and Color-Related Objects / Using Colors and Color-Related Objects


Manipulating the Colors in a Color Set Object

If you are using indexed color space, you can gain access to the array of colors in the space's color set or to any contiguous subset of the colors in the array. You can then inspect, rearrange, modify, or add or delete colors from the array.

For example, suppose you want to sort the colors in a color set so that they will
display in a visually useful manner in a palette for the user. You could first call the GXGetColorSet function to get the array of colors. You could then sort the colors (say, by hue (H) in gxHSVSpace), and then return the array to the color set by calling the GXSetColorSet function.

Alternatively, suppose you already have a luminance-sorted array of colors in a color set, and you want to convert the first (darkest) color in the array to pure black. Instead of accessing the entire array, you can call GXGetColorSetParts to get only the first color in the array. You can then change that color to black, and reinsert it in the color set by calling GXSetColorSetParts.

To add colors to or delete colors from a color set, call GXGetColorSet, modify the color-value array as needed, and then call GXSetColorSet to place the new array in
the color set.

To change the color space of a color set, follow this sequence of calls:

Remember that simply changing the color space of a color set does not convert the individual color values from one space to the other.

As an example of color-set manipulation, the following code fragment from a drawing routine matches each of the colors of a color set used by the shape matchShape to a specific color profile (qmsProfile). The code uses the GXGetColorSet function to fill out a temporary array of color values (mycolors) from the color set, converts each color (from RGB space with a nil profile to RGB space with qmsProfile, in this case) with the GXConvertColor function, and then reassigns the color values to the color set
with the GXSetColorSet function.

gxSetColor mycolors[256];
oldColorCount = GXGetColorSet(GXGetShapeColorSet(matchShape), 
                              nil, mycolors);
for (i = 0; i < oldColorCount; i++) 
{
   gxColor tmpColor;
   tmpColor.space = gxRGBSpace;
   tmpColor.profile = nil;
   tmpColor.element.rgb = mycolors[i].rgb;
   GXConvertColor(&tmpColor,gxRGBSpace, nil, qmsProfile);
   mycolors[i].rgb = tmpColor.element.rgb;
}
GXSetColorSet(GXGetShapeColorSet(matchShape), gxRGBSpace, 
               oldColorCount, mycolors);
The GXGetColorSet function is described on page 4-73. The GXSetColorSet function is described on page 4-74. The GXGetColorSetParts function is described on page 4-75. The GXSetColorSetParts function is described on page 4-76.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
7 JUL 1996